/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.newnode.model;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import offset.nodes.client.dialog.newnode.view.FilePanel;
import offset.nodes.client.model.ServerModel;

/**
 *
 * @author Walter Lütgenau
 */
public class FileNewNodeConfigurator extends AbstractNewNodeConfigurator implements NewNodeConfigurator {

    @Override
    public ObjectType getObjectType() {
        ContentType[] fileContentTypes = new ContentType[]{
            new ContentType("", "none", "application/octet-stream", getBundle()),
            new ContentType("binary", "binary", "application/octet-stream", getBundle()),
            new ContentType("css", "css", "text/css", getBundle()),
            new ContentType("js", "js", "text/javascript", getBundle()),
            new ContentType("html", "html", "text/html", getBundle()),
            new ContentType("png", "png", "image/png", getBundle()),
            new ContentType("gif", "gif", "image/gif", getBundle()),
            new ContentType("tif", "tiff", "image/tiff", getBundle()),
            new ContentType("jpg", "jpeg", "image/jpeg", getBundle())
        };

        FilePanel filePanel = new FilePanel();
        filePanel.init(fileContentTypes, null);
        
        return new ObjectType(bundle, "file.name", "file.description", new Step[]{new Step(bundle, "file.step1.name", filePanel)}, 
                new ServiceProcessor(getService(), getPath()) {

            public URL process() {
                NewFile.Request request = new NewFile.Request();
                FilePanel panel = (FilePanel) getObjectType().getSteps()[1].getPanel();

                request.setPath(getPath() + "/" + panel.getFileName());
                request.setContentType((String) panel.getContentType());
                File contentFile = new File(panel.getFilePath());
                if (!contentFile.exists())
                    return null;
                byte[] buf = new byte[(int) contentFile.length()];
                try {
                    FileInputStream in = new FileInputStream(contentFile);
                    in.read(buf);
                    in.close();
                    request.setContent(buf);

                    ServerModel model = new ServerModel(getService());
                    model.sendRequest(request);

                    return null;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        });

    }
}
